﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsStateService
{
    public class LogManager
    {
        EventLog eventLog;
        public LogManager() 
        {
            eventLog = new EventLog();
        }

        private static LogManager _instance;
        public static LogManager Instance 
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new LogManager();
                }
                return _instance;
            }
        }

        public IList<LogData> GetAllApplicationLogs()
        {
            eventLog.Log = "Application";
            EventLogEntryCollection collection = eventLog.Entries;
            IList<LogData> logDatas = new List<LogData>();
            foreach (EventLogEntry item in collection)
            {
                LogData data = new LogData();
                data.LogLevel = item.EntryType;
                data.DateTime = item.TimeWritten;
                data.UserName = item.UserName;
                data.Content = item.Message;
                data.Source = item.Source;
                data.EventId = item.InstanceId;
                data.Index = item.Index;
                data.LogType = 1;
                logDatas.Add(data);
            }
            return logDatas;
        }

        public IList<LogData> GetAllSystemLogs()
        {
            eventLog.Log = "System";
            EventLogEntryCollection collection = eventLog.Entries;
            IList<LogData> logDatas = new List<LogData>();
            //System.Diagnostics.Debugger.Launch();
            foreach (EventLogEntry item in collection)
            {
                LogData data = new LogData();
                data.LogLevel = item.EntryType;
                data.DateTime = item.TimeWritten;
                data.UserName = item.UserName;
                data.Content = item.Message;
                data.Source = item.Source;
                data.EventId = item.InstanceId;
                data.Index = item.Index;
                data.LogType = 0;
                logDatas.Add(data);
            }
            return logDatas;
        }

        public IList<LogData> GetAllLogs()
        {
            IList<LogData> app_logs = GetAllApplicationLogs();
            IList<LogData> sys_logs = GetAllSystemLogs();
            app_logs.ToList().AddRange(sys_logs);
            return app_logs;
        }

        public IList<LogData> GetLogsByTime(DateTime dt)
        {
            eventLog.Log = "System";
            EventLogEntryCollection collection = eventLog.Entries;
            IList<LogData> logDatas = new List<LogData>();
            //System.Diagnostics.Debugger.Launch();
            int count = collection.Count;
            for(int i = count -1; i>=0;i--)
            {
                EventLogEntry item = collection[i];
                DateTime item_time = item.TimeWritten;
                if (dt.Year == item_time.Year && dt.Month == item_time.Month && dt.Day == item_time.Day)
                {
                    LogData data = new LogData();
                    data.LogLevel = item.EntryType;
                    data.DateTime = item.TimeWritten;
                    data.UserName = item.UserName;
                    data.Content = item.Message;
                    data.Source = item.Source;
                    data.EventId = item.EventID;
                    data.Index = item.Index;
                    data.LogType = 0;
                    logDatas.Add(data);
                }
            }

            eventLog.Log = "Application";
            collection = eventLog.Entries;
            count = collection.Count;
            for (int i = count - 1; i >= 0; i--)
            {
                EventLogEntry item = collection[i];
                DateTime item_time = item.TimeWritten;
                if (dt.Year == item_time.Year && dt.Month == item_time.Month && dt.Day == item_time.Day)
                {
                    LogData data = new LogData();
                    data.LogLevel = item.EntryType;
                    data.DateTime = item.TimeWritten;
                    data.UserName = item.UserName;
                    data.Content = item.Message;
                    data.Source = item.Source;
                    data.EventId = item.EventID;
                    data.Index = item.Index;
                    data.LogType = 1;
                    logDatas.Add(data);
                }
            }
            return logDatas;
        }
    }
}
